import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageService } from './../../../../providers/StorageService';
import { UserService } from './../../../../providers/UserService';
import { FileObj } from "./../../../../model/FileObj";
import { BasePage } from './../../../basePage';
import { nailerService } from './../../../../providers/nailerService';
import { NativeService } from './../../../../providers/NativeService';
import { FileService } from "./../../../../providers/FileService";
/**
 * Generated class for the ShopCompilePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-shop-compile',
  templateUrl: 'shop-compile.html',
})
export class ShopCompilePage extends BasePage {
  cityData: any[] = []; //城市数据
  cityName: string = '湖北省-武汉市-洪山区'; //初始化城市名

  userinfo = {};//获取内存中的数据
  userVo = {};//获取后台返回回来的数据
  userApi = { //需要修改的值
    shop_id: 0,
    shop_logo: '',
    backlogo: '',
    shop_name: '',
    shop_address: '',
    shop_tel: '',
    shop_province:0,
    shop_city: 0,
    shop_area:0,
  };

  rootpath='';

  // imgpath="";
  fileObjList=[];   //图片列表,与fileObjListChange形成双向数据绑定
  imgpathlogo = '';     //显示门店logo
  imgpathbg = '';     //显示门店背景

  logoclass='logo';
  imgpathbgclass='imgpathbg';

  constructor(injector: Injector, public userService: UserService, public nailerservice: nailerService, public stc: StorageService, public nativeService: NativeService, private fileService: FileService) {
    super(injector);
    this.userinfo = this.stc.read('userinfo');
    this.userApi.shop_id = this.userinfo['shop_id'];
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ShopCompilePage');
    this.nativeService.geCityData().subscribe(res => {
      this.cityData = res;
    });
    this.myShopInfoAPI();
  }

 dismissCLI() {
    this.navCtrl.pop()
  }
  /**获取店铺资料 */
  async myShopInfoAPI() {
    let param = { 'user_id': this.userinfo['user_id'] };
    console.log(param);
    let result = await this.nailerservice.myShopInfoAPI(param);
    console.log(result);
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: '获取店铺资料失败',
        duration: 3000,
        position: 'top',
      });
    } else {
      this.userVo = result.data;
      this.imgpathlogo=this.userVo['shop_logo'];
      this.imgpathbg=this.userVo['backlogo'];
      this.cityName = this.userVo['locale'];
      this.userApi.shop_name = this.userVo['shop_name'];
      this.userApi.shop_address = this.userVo['shop_address'];
      this.userApi.shop_tel = this.userVo['shop_tel'];
      this.userApi.shop_logo=this.imgpathlogo;
      this.userApi.backlogo=this.imgpathbg;
      this.userApi.shop_province = this.userVo['shop_province'];
      this.userApi.shop_city = this.userVo['shop_city'];
      this.userApi.shop_area =this.userVo['shop_area'];
      //this.imgpath=PIC_URL+this.userVo['shop_logo'];
    }
  }

  InputCLI(type, inputname, placeholder, fontnum, inputvalue, field) {
    this.openInputText(type, inputname, placeholder, fontnum, inputvalue, data => {
      if (data) {
        this.userApi[field] = data;
        this.userVo[field] = data;
      } else {
        return;
      }

    });
    
  }

  /**修改图像 */
  publicok() {
    this.editMyShopInfoAPI();
  }


/**子页面返回处理数据 */
  returnapiimg(event){
    console.log("我是父页面");
    console.log(event);
    // if (event.code != 1) {
    //   let toast = this.toastCtrl.create({
    //     message: event.msg,
    //     duration: 3000,
    //     position: 'top',
    //   })
    //   toast.present();
    // }else{
      this.fileObjList=event;
      console.log('我是data里面的数据'+this.fileObjList[0])
      if(event.flagbs=='logo'){
        console.log('进来了');
        let shop_logname=this.fileObjList[0];
        console.log('shop_loginname'+shop_logname);
        this.userApi.shop_logo=shop_logname;
        console.log('this.userApi.shop_logo'+this.userApi.shop_logo);
      }
      if(event.flagbs == 'pictureadd'){
        this.userApi.backlogo=this.fileObjList[0];
      }
      console.log("我是返回回来的数据")
      console.log(this.userApi);

    // }
  }


  ceshi(event) {
    console.log(event);
  }

  //城市选择器被改变时触发的事件
  cityChange(event) {
    this.userApi.shop_province = event.province.value;
    this.userApi.shop_city = event.city.value;
    this.userApi.shop_area = event.region.value;
  }


  async editMyShopInfoAPI() {
    console.log(this.userApi);
    
    var re = /^1[3|4|5|6|7|8|9]\d{9}$/;
    if(this.userApi['shop_name'] ==''){
      let toast = this.toastCtrl.create({
        message: '店铺名称不能为空！',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(this.userApi['shop_address'] ==''){
      let toast = this.toastCtrl.create({
        message: '详细地址不能为空！',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(this.userApi['backlogo'] ==''){
      let toast = this.toastCtrl.create({
        message: '背景图不能为空！',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(this.userApi['shop_logo'] == ''){
      let toast = this.toastCtrl.create({
        message: '店铺头像不能为空！',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if( this.userApi['shop_tel'] ==''){
      let toast = this.toastCtrl.create({
        message: '联系方式不能为空',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if (!re.test(this.userApi.shop_tel)) {
      let toast = this.toastCtrl.create({
        message: '电话号码格式不正确',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(!/^[\u4e00-\u9fa50-9A-Za-z]+$/im.test(this.userApi['shop_name'])){
      let toast = this.toastCtrl.create({
        message: '店铺名称不允许有特殊字符！',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else if(this.userVo['backlogo'] == this.userApi['backlogo']  &&
            this.userVo['shop_address'] == this.userApi['shop_address']  &&
            this.userVo['shop_area']  == this.userApi['shop_area']  &&
            this.userVo['shop_city']  == this.userApi['shop_city']  &&
            this.userVo['id']  == this.userApi['shop_id']  &&
            this.userVo['shop_logo'] == this.userApi['shop_logo']  &&
            this.userVo['shop_name']  == this.userApi['shop_name'] &&
            this.userVo['shop_province'] == this.userApi['shop_province']  &&
            this.userVo['shop_tel']  == this.userApi['shop_tel'] )
    {
      let toast = this.toastCtrl.create({
        message: '未作修改',
        duration: 1000,
        position: 'bottom',
      })
      toast.present();
      setTimeout(()=>{
        this.navCtrl.push('MyShopPage');
      },1000)
    }else{
      let result = await this.nailerservice.editMyShopInfoAPI(this.userApi);
      if (result.code == 1) {
        let toast = this.toastCtrl.create({
          message: result.msg,
          duration: 1000,
          position: 'bottom',
        })
        toast.present();
        setTimeout(()=>{
          this.navCtrl.pop();
        },1000)
      } else {
        let toast = this.toastCtrl.create({
          message: result.msg,
          duration: 2000,
          position: 'bottom',
        })
        toast.present();
      }
    }
  }


}
